GameTimer = {}

local this = GameTimer
GameTimer.__index = GameTimer

function GameTimer.StartTimer(object, func, delayTime, ...)
	local timer = {}
	setmetatable(timer, GameTimer)

	timer.object = object
	timer.func = func
	timer.delayTime = delayTime
	timer.param = {...}

	UpdateBeat:Add(timer.update, timer)
	table.insert(this, timer)
end

function GameTimer.StopAllTimer()
	for k,v in pairs(this) do
		v:stopTimer()
 	end

 	tableUtil.clearTable(this)
end

function GameTimer:stopTimer()
	self.object = nil
	self.func = nil
	self.delayTime = 0.0
	self.param = nil

	UpdateBeat:Remove(self.update, self)
	this.Remove(self)
end

function GameTimer:update()
	self.delayTime = self.delayTime - Time.deltaTime 
	if(self.delayTime <= 0.0) then
		self.func(self.object, self.param)
		self:stopTimer()
	end
end

function GameTimer.Remove(timer)
	local index = -1
	for k,v in ipairs(this) do
		if(v == timer) then
			index = k
		end
	end

	if( index > 0 ) then
		table.remove(this, index)
	end
end

